package com.lcj.springems.service.impl;

import com.lcj.springems.dao.EmpDao;
import com.lcj.springems.entity.Emp;
import com.lcj.springems.exception.DataAccessException;
import com.lcj.springems.exception.RequestParameterException;
import com.lcj.springems.exception.ServiceException;
import com.lcj.springems.service.EmpService;
import com.lcj.springems.utils.NullUtil;

import java.util.List;


public class EmpServiceImpl implements EmpService {

    private EmpDao empDao;

    public EmpDao getEmpDao() {
        return empDao;
    }

    public void setEmpDao(EmpDao empDao) {
        this.empDao = empDao;
    }

    @Override
    public void addEmp(String name, String salary, String gender, String edu, String[] hobbies) throws RequestParameterException, ServiceException {
        if(NullUtil.isNull(name)||NullUtil.isNull(salary)||NullUtil.isNull(gender)||NullUtil.isNull(edu))
            throw new RequestParameterException("请求参数有误");
        Emp emp = new Emp(name,Double.parseDouble(salary),gender,edu,hobbies);
        try {
            empDao.insert(emp);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        } catch (NumberFormatException e)
        {
            e.printStackTrace();
            throw new RequestParameterException("工资格式有误");
        }

    }

    @Override
    public void removeEmp(String id) throws RequestParameterException, ServiceException {
        if(NullUtil.isNull(id))
        {
            throw new RequestParameterException("请求参数有误");
        }
        try {
            empDao.delete(Integer.parseInt(id));
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void modifyEmp(String id, String name, String salary, String gender, String edu, String[] hobbies) throws RequestParameterException, ServiceException {
        if(NullUtil.isNull(id)||NullUtil.isNull(name)||NullUtil.isNull(salary)||NullUtil.isNull(gender)||NullUtil.isNull(edu))
            throw new RequestParameterException("请求参数有误");
        Emp emp = new Emp(Integer.parseInt(id),name,Double.parseDouble(salary),gender,edu,hobbies);
        try {
            empDao.update(emp);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public Emp getEmpById(String id) throws RequestParameterException, ServiceException {
        if(NullUtil.isNull(id))
            throw new RequestParameterException("请求参数有误");
        try {
            Emp emp = empDao.selectById(Integer.parseInt(id));
            return emp;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public List<Emp> getAll() throws ServiceException {
        try {
            List<Emp> emps = empDao.selectAll();
            return emps;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }
}
